package xy.come.juhenews.Activity;

import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.zhy.autolayout.AutoLinearLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import pub.devrel.easypermissions.AfterPermissionGranted;
import pub.devrel.easypermissions.EasyPermissions;
import xy.come.juhenews.Base.BaseActivity;
import xy.come.juhenews.R;
import xy.come.juhenews.Uitils.ToastUtilsX;
import xy.come.juhenews.Uitils.ZXingUtils;

/**
 * Created by Xia_焱 on 2018/8/15.
 * e-mail：XiahaotianV@163.com
 */

public class QrCodeActivity extends BaseActivity implements EasyPermissions.PermissionCallbacks {
    private static final int REQUEST_CODE_QR_CODE_PERMISSIONS = 1;
    @BindView(R.id.head_title)
    TextView headTitle;
    @BindView(R.id.ll_head_back)
    LinearLayout llHeadBack;
    @BindView(R.id.ll_scan_code)
    AutoLinearLayout llScanCode;
    @BindView(R.id.ll_create_code)
    AutoLinearLayout llCreateCode;
    @BindView(R.id.ll_line_code)
    AutoLinearLayout llLineCode;
    @BindView(R.id.tv_scan_result)
    TextView tvScanResult;
    @BindView(R.id.et_two)
    EditText etTwo;
    @BindView(R.id.iv_two)
    ImageView ivTwo;
    @BindView(R.id.ll_two_code)
    AutoLinearLayout llTwoCode;
    @BindView(R.id.et_one)
    EditText etOne;
    @BindView(R.id.iv_one)
    ImageView ivOne;
    @BindView(R.id.ll_one_code)
    AutoLinearLayout llOneCode;
    @BindView(R.id.iv_two_code)
    ImageView ivTwoCode;
    @BindView(R.id.iv_one_code)
    ImageView ivOneCode;

    @Override
    public int getLayoutId() {
        return R.layout.activity_qr_code;
    }

    @Override
    protected void initView() {
        headTitle.setText("扫码工具");
    }

    @Override
    protected void initData() {

    }


    @Override
    protected void onStart() {
        super.onStart();
        requestCodeQRCodePermissions();
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, List<String> perms) {
    }

    @Override
    public void onPermissionsDenied(int requestCode, List<String> perms) {
    }

    @AfterPermissionGranted(REQUEST_CODE_QR_CODE_PERMISSIONS)
    private void requestCodeQRCodePermissions() {
        String[] perms = {Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE};
        if (!EasyPermissions.hasPermissions(this, perms)) {
            EasyPermissions.requestPermissions(this, "扫描二维码需要打开相机和散光灯的权限", REQUEST_CODE_QR_CODE_PERMISSIONS, perms);
        }
    }


    @OnClick({R.id.ll_head_back, R.id.ll_scan_code, R.id.ll_create_code, R.id.ll_line_code, R.id.iv_two, R.id.iv_one})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_head_back:
                finish();
                break;
            case R.id.ll_scan_code:
                Intent TestScanIntent = new Intent(QrCodeActivity.this, TestScanActivity.class);
                startActivityForResult(TestScanIntent, 100);
                break;
            case R.id.ll_create_code:
                llOneCode.setVisibility(View.GONE);
                ivOneCode.setVisibility(View.GONE);
                llTwoCode.setVisibility(View.VISIBLE);
                ivTwoCode.setVisibility(View.VISIBLE);
                break;
            case R.id.ll_line_code:
                llTwoCode.setVisibility(View.GONE);
                ivTwoCode.setVisibility(View.GONE);
                llOneCode.setVisibility(View.VISIBLE);
                ivOneCode.setVisibility(View.VISIBLE);
                break;

            case R.id.iv_two:
                if (TextUtils.isEmpty(etTwo.getText().toString().trim())) {
                    ToastUtilsX.showLongToast(QrCodeActivity.this, "输入内容不能为空！");
                    return;
                }
                Bitmap bitmap = ZXingUtils.createQRImage(etTwo.getText().toString().trim(), ivTwoCode.getWidth(), ivTwoCode.getHeight());
                ivTwoCode.setImageBitmap(bitmap);
                break;
            case R.id.iv_one:
                if (TextUtils.isEmpty(etOne.getText().toString().trim())) {
                    ToastUtilsX.showLongToast(QrCodeActivity.this, "输入内容不能为空！");
                    return;
                }
                Bitmap bitmapX = ZXingUtils.creatBarcode(QrCodeActivity.this, etOne.getText().toString().trim(), ivOne.getWidth(), ivOne.getHeight(), false);
                ivOneCode.setImageBitmap(bitmapX);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 100 && resultCode == 200) {
            String result = data.getStringExtra("result");
            tvScanResult.setVisibility(View.VISIBLE);
            tvScanResult.setText("扫描结果为：" + result);
        }
    }


}
